<?php
/**
 * l10n.php
 * 
 * The Session class is meant to simplify the task of keeping
 * track of logged in users and also guests.
 *
 * Written by: A. Sari
 * $Id: l10n.php 14 2009-07-15 21:20:35Z ahmtsari $
 */
class l10n
{
	var $messages;
	var $I18N = Array();
	    
    function l10n() {
    	global $LANG;
    	$this->messages = array (
		    'en_US'=> array(
		       'My favorite foods are' =>
		           'My favorite foods are',
		       'french fries' => 'french fries',
		       'biscuit' => 'biscuit',
		       'candy' => 'candy',
		       'potato chips' => 'potato chips',
		       'cookie' => 'cookie',
		       'corn' => 'corn',
		       'eggplant' => 'eggplant'
		    ),
		
		    'en_GB'=> array(
		        'My favorite foods are' =>
		            'My favourite foods are',
		        'french fries' => 'chips',
		        'biscuit' => 'scone',
		        'candy' => 'sweets',
		        'potato chips' => 'crisps',
		        'cookie' => 'biscuit',
		        'corn' => 'maize',
		        'eggplant' => 'aubergine'
		    ));		
		    
		    $this->setLocale($LANG);
		    	
    }
    
    
    function msg($s) {
	    global $LANG;
	    
	    if (isset($this->messages[$LANG][$s])) {
	        return $this->messages[$LANG][$s];
	    } else {
	        //error_log("l10n error:LANG:" . "$LANG, message: '$s'");
	        return $s;
	    }
	}   
	
	function define($key, $value) {
		$this->I18N[$key] = $value;
	}
	
	function exists($key) {
		return isset($this->I18N[$key]);
	}
	
	function get($key, $defaultValue = NULL) {
		return (isset($this->I18N[$key]) ? $this->I18N[$key] : ($defaultValue != NULL ? $defaultValue : $key));
	}
	
	public function setLocale($locale) {
		global $template, $smarty;
		$_SESSION["_____locale"] = $locale;
		if (file_exists(FS_ROOT . '/includes/i18n/' . $locale )) {
			$systemArr = parse_ini_file(FS_ROOT . '/includes/i18n/' . $locale );
			while (list($k, $v) = each ($systemArr))
				$this->define($k, $v);
		}
		if (file_exists($smarty->template_dir . "/i18n/" . $locale )) {
			$systemArr = parse_ini_file($smarty->template_dir . "/i18n/" . $locale );
			while (list($k, $v) = each ($systemArr))
				$this->define($k, $v);
		}		
	}
	
	public function getLocale($defaultLocale = "tr") {
        $locale = $_SESSION("_____locale");
        if ($locale != NULL) {
        	  return strtolower($locale);
        }
        return $defaultLocale;		
	}
	
};
?>